<template>
  <div id="main">
    <h2>执行情况管理</h2>

    <div class="head">
      <el-button type="primary" style="margin:20px" @click="handleOpen('添加执行记录', 1)">添加执行记录</el-button>
      <el-input v-model="searchTableInfo" placeholder="请输入搜索内容" style="width:240px;float: right;margin:20px;" />
    </div>
    <div id="table-area">
      <el-table
        v-loading="listLoading"
        :data="list.slice((currentPage - 1) * pagesize, currentPage * pagesize)"
        border
        fit
        highlight-current-row
      >
        <el-table-column
          label="序号"
          type="index"
          align="center"
        >
          <template scope="scope">
            <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="studentName"
          label="学生姓名"
          width="220"
        />
        <el-table-column
          align="center"
          prop="staffName"
          label="执行教师"
          width="220"
        />
        <el-table-column
          align="center"
          prop="excuteStarttime"
          label="开始时间"
          width="220"
        />
        <el-table-column align="center" label="操作">
          <template slot-scope="Scope">
            <el-button type="primary" icon="el-icon-edit" @click="handleOpen('修改记录', 2, Scope.row.excuteId)">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" @click=" handleDelete(Scope.row.excuteId,Scope.row.studentName)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div id="pages">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[5, 10]"
          :page-size="pagesize"
          layout="total, sizes,prev, pager, next"
          :total="list.length"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <el-dialog title="" :close-on-click-modal="false" :visible.sync="dialogFormVisible" :before-close="handleClose">
      <el-form ref="excute" :model="excute" label-position="top" :rules="rules">
        <el-form-item prop="excuteStudentId" label="学生姓名" :label-width="formLabelWidth">
          <el-select v-model="excute.excuteStudentId" filterable placeholder="请选择" style="width: 40%;">
            <el-option
              v-for="item in child"
              :key="item.studentId"
              :label="item.String"
              :value="item.studentId"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="excuteTeacherId" label="执行教师" :label-width="formLabelWidth">
          <el-select v-model="excute.excuteTeacherId" filterable placeholder="请选择" style="width: 40%;">
            <el-option
              v-for="item in staff"
              :key="item.staffId"
              :label="item.String"
              :value="item.staffId"
            />
          </el-select>
        </el-form-item>

        <el-form-item prop="excuteDaytime" label="天数" :label-width="formLabelWidth">
          <el-select v-model="excute.excuteDaytime" filterable placeholder="请选择第几天" style="width: 40%;">
            <el-option
              v-for="item in excuteDaytimes"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item prop="excuteStarttime" label="开始日期">
          <div class="block">
            <div class="demonstration">{{ excuteStarttime }}</div>
            <el-date-picker
              v-model="excute.excuteStarttime"
              type="date"
              placeholder="选择时间"
              format="yyyy -MM - dd hh : mm: ss"
              value-format="yyyy-MM-dd hh:mm:ss"
              style="width:245px"
            />
          </div>
        </el-form-item>
        <el-form-item prop="excuteProject" label="教学项目" :label-width="formLabelWidth">
          <el-input v-model="excute.excuteProject" autocomplete="off" maxlength="32" />
        </el-form-item>

        <el-form-item prop="excuteTeachtarget" label="教学目标" :label-width="formLabelWidth">
          <el-input v-model="excute.excuteTeachtarget" autocomplete="off" maxlength="32" />
        </el-form-item>

        <el-form-item prop="excuteExpression" label="学生表现" :label-width="formLabelWidth">
          <el-input v-model="excute.excuteExpression" autocomplete="off" maxlength="32" />
        </el-form-item>

        <el-form-item prop="excuteIssue" label="问题及解决策略" :label-width="formLabelWidth">
          <el-input v-model="excute.excuteIssue" autocomplete="off" maxlength="32" />
        </el-form-item>

        <el-form-item prop="excuteSummarize" label="教学总结" :label-width="formLabelWidth">
          <el-input v-model="excute.excuteSummarize" autocomplete="off" maxlength="32" />
        </el-form-item>

        <el-form-item prop="excutePlantarget" label="计划目标" :label-width="formLabelWidth">
          <el-input v-model="excute.excutePlantarget" autocomplete="off" maxlength="32" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleClose">取 消</el-button>
        <el-button v-if="editType == 2" type="primary" @click="handleUpdate('excute')">修 改</el-button>
        <el-button v-if="editType == 1" type="primary" @click="handleAddUser('excute')">添 加</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getAllexcute, addexcute, deleteexcute, updateexcute, getexcuteById } from '@/api/excute'
import { selectStudent } from '@/api/child'
import { selectstaff } from '@/api/staff'

export default {
  data() {
    return {
      excuteDaytimes: [
        {
          value: 0,
          label: '第一天'
        },
        {
          value: 1,
          label: '第二天'
        },
        {
          value: 2,
          label: '第三天'
        },
        {
          value: 3,
          label: '第四天'
        },
        {
          value: 4,
          label: '第五天'
        }
      ],
      excuteStarttime: '',
      newexcute: {
        studentName: '',
        staffName: '',
        excuteStarttime: '',
        excuteId: '',
        excuteDaytime: ''
      },
      value: '',
      currentPage: 1, // 默认显示页面为1
      pagesize: 5, //    每页的数据条数
      // list: [],
      tableList: [],
      searchTableInfo: '',
      excute: {},
      listLoading: false,
      dialogFormVisible: false,
      editType: 1, // 1 添加 2 修改
      child: {},
      staff: {},
      formLabelWidth: '200px',
      rules: {
        excuteStudentId: [
          { required: true, message: '儿童姓名不能为空', trigger: 'blur' }
        ],
        excuteDaytime: [
          { required: true, message: '天数不能为空', trigger: 'blur' }
        ],
        excuteExpression: [
          { required: true, message: '儿童表现不能为空', trigger: 'blur' }
        ],
        excuteIssue: [
          { required: true, message: '问题及解决方案不能为空', trigger: 'blur' }
        ],
        excutePlantarget: [
          { required: true, message: '计划目标不能为空', trigger: 'blur' }
        ],
        excuteProject: [
          { required: true, message: '教学项目不能为空', trigger: 'blur' }
        ],
        excuteStarttime: [
          { required: true, message: '开始时间不能为空', trigger: 'blur' }
        ],
        excuteSummarize: [
          { required: true, message: '总结不能为空', trigger: 'blur' }
        ],
        excuteTeacherId: [
          { required: true, message: '执行教师不能为空', trigger: 'blur' }
        ],
        excuteTeachtarget: [
          { required: true, message: '教学目标不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    // 根据计算属性模糊搜索
    list() {
      const searchTableInfo = this.searchTableInfo
      console.log(this.searchTableInfo)
      if (searchTableInfo) {
        // filter() 方法创建一个新的数组，新数组中的元素是通过检查指定数组中符合条件的所有元素。
        // 注意： filter() 不会对空数组进行检测。
        // 注意： filter() 不会改变原始数组。
        return this.tableList.filter(data => {
          console.log('success' + data)
          // some() 方法用于检测数组中的元素是否满足指定条件;
          // some() 方法会依次执行数组的每个元素：
          // 如果有一个元素满足条件，则表达式返回true , 剩余的元素不会再执行检测;
          // 如果没有满足条件的元素，则返回false。
          // 注意： some() 不会对空数组进行检测。
          // 注意： some() 不会改变原始数组。
          return Object.keys(data).some(key => {
            // indexOf() 返回某个指定的字符在某个字符串中首次出现的位置，如果没有找到就返回-1；
            // 该方法对大小写敏感！所以之前需要toLowerCase()方法将所有查询到内容变为小写。
            return (
              String(data[key])
                .toLowerCase()
                .indexOf(searchTableInfo) > -1
            )
          })
        })
      }
      return this.tableList
    }
  },
  created() {
    this.fetchData()
    this.getinfo()
  },
  methods: {
    // 每页下拉显示数据
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    getinfo: function() {
      selectStudent().then((resp) => {
        this.child = resp.obj
        console.log(this.child)
      })
      selectstaff().then((resp) => {
        this.staff = resp.obj
        console.log(this.staff)
      })
    },
    handleClick(row) {
      console.log(row)
    },
    fetchData() {
      this.tableList = []
      this.listLoading = true
      getAllexcute().then(data => {
        this.newexcute
        for (var i = 0; i < data.obj.length; i++) {
          this.newexcute.studentName = data.obj[i].student.studentName
          this.newexcute.staffName = data.obj[i].staff.staffName
          this.newexcute.excuteStarttime = data.obj[i].excuteStarttime
          this.newexcute.excuteId = data.obj[i].excuteId
          // this.newexcute.excutePlantarget = data.obj[i].excutePlantarget
          // this.newexcute.excuteProject = data.obj[i].excuteProject
          // this.newexcute.excuteSummarize = data.obj[i].excuteSummarize
          // this.newexcute.excuteTeachtarget = data.obj[i].excuteTeachtarget
          // this.newexcute.excuteExpression = data.obj[i].excuteExpression

          // if (data.obj[i].excuteDaytime === 0) {
          //   this.newexcute.excuteDaytime = '第一天'
          // }
          // if (data.obj[i].excuteDaytime === 1) {
          //   this.newexcute.excuteDaytime = '第二天'
          // }
          // if (data.obj[i].excuteDaytime === 2) {
          //   this.newexcute.excuteDaytime = '第三天'
          // }
          // if (data.obj[i].excuteDaytime === 3) {
          //   this.newexcute.excuteDaytime = '第四天'
          // }
          // if (data.obj[i].excuteDaytime === 4) {
          //   this.newexcute.excuteDaytime = '第五天'
          // }
          this.tableList.push(this.newexcute)
          this.newexcute = {}
        }
        console.log(this.tableList)
        this.listLoading = false
      })
    },
    handleDelete(id, username) {
      this.$confirm('确认删除该记录？该学生名字为' + username).then(() =>
        deleteexcute(id).then(data => {
          this.$notify({
            title: '操作成功',
            type: 'success'
          })
          const totalPage = Math.ceil(
            (this.tableList.length - 1) / this.pagesize
          )
          const page =
            this.currentPage > totalPage ? totalPage : this.currentPage
          this.currentPage = this.currentPage < 1 ? 1 : page
          this.fetchData()
        })
      )
    },
    handleAddUser(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          addexcute(this.excute).then(resp => {
            this.$notify({
              title: '操作成功',
              type: 'success'
            })
            this.fetchData()
          })
          this.excute = {}
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    },
    handleOpen(title, type, id) {
      this.excute = {}
      this.title = title
      this.editType = type
      if (type === 2) {
        getexcuteById(id).then((resp) => {
          this.excute = resp.obj
        })
        this.dialogFormVisible = true
      } else {
        this.dialogFormVisible = true
      }
    },
    // getstudents() {
    //   getStudentString(this.excute.classId).then(resp => {
    //     this.child = resp.obj;
    //   });
    // },
    // getstudents2(value) {
    //   getStudentString(this.excute.classId).then(resp => {
    //     this.child = resp.obj;
    //   });
    //   this.excute.studentId = "";
    // },
    handleClose() {
      this.dialogFormVisible = false
      this.excute = {}
    },
    // 天数
    excuteDaytime(row, column) {
      console.log(this.row)
      if (row.excuteDaytime === 0) {
        return '第一天'
      }
      if (row.excuteDaytime === 1) {
        return '第二天'
      }
      if (row.excuteDaytime === 2) {
        return '第三天'
      }
      if (row.excuteDaytime === 3) {
        return '第四天'
      }
      if (row.excuteDaytime === 4) {
        return '第五天'
      }
    },
    handleUpdate(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log(this.excute)
          updateexcute(this.excute).then(resp => {
            this.$notify({
              title: '修改成功',
              type: 'success'
            })
            this.fetchData()
          }).catch(resp => {
            // this.$notify({
            //  title:  "修改成功",
            //  type: "success"
            // });
            this.fetchData()
          })
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    }

    // getAllJurisdiction() {
    //   getAllJurisdiction().then((resp) => {
    //     this.jurisdiction = resp.obj
    //   })
    // },
  }
}
</script>

<style scoped>
#main {
  margin-left: 5px;
  margin-right: 5px;
}
#table-area {
  margin-left: 10px;
  margin-right: 10px;
}
</style>
